﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;

namespace DBModel
{
    public partial class CurriculumVitae
    {
        public override string ToString()
        {
            StringBuilder result = new StringBuilder(string.Format("{0}", Presentation));

            if (Employment.Count > 0)
                result.Append("<br>Employments:");
            foreach (Employment employment in Employment)
                result.Append(string.Format("\r\n{0}", employment));

            if (Education.Count > 0)
                result.Append("\r\n\r\nEducations:");
            foreach (Education education in Education)
                result.Append(string.Format("\r\n{0}", education));

            if (OtherQualification.Count > 0)
                result.Append("\r\n\r\nOther Qualifications:");
            foreach (OtherQualification otherQualification in OtherQualification)
                result.Append(string.Format("\r\n{0}", otherQualification));

            if (Skill.Count > 0)
                result.Append("\r\n\r\nSkills:");
            foreach (Skill skill in Skill)
                result.Append(string.Format("\r\n{0}", skill));

            if (Language.Count > 0)
                result.Append("\r\n\r\nLanguages:");
            foreach (Language language in Language)
                result.Append(string.Format("\r\n{0}", language));

            return result.ToString();
        }

        public string ToHtml()
        {
            StringBuilder result = new StringBuilder();

            result.Append("<div id='cv'>");
            result.Append("<div class='applicant-info'>");
            result.Append("<p><b>Namn:</b> ");
            result.Append(string.Format("{0} {1}</p>", Applicant.FirstName, Applicant.LastName));
            result.Append("</div>");
            result.Append("<div class='presentation'>");
            result.Append("<h3>Personlig presentation</h3>");
            result.Append(string.Format("{0}", Presentation));
            result.Append("</div>");

            result.Append("<div class='employments'>");
            result.Append("<h4>Anställningar:</h4>");
            if (Employment.Count > 0)
            {
                foreach (Employment employment in Employment)
                    result.Append(string.Format("{0}", employment.ToHtml()));
            }
            result.Append("</div>");

            result.Append("<div class='educations'>");
            result.Append("<h4>Utbildningar:</h4>");
            if (Education.Count > 0)
            {
                foreach (Education education in Education)
                    result.Append(string.Format("{0}", education.ToHtml()));
            }
            result.Append("</div>");

            result.Append("<div class='skills'>");
            result.Append("<h4>Kompetencer:</h4>");
            if (Skill.Count > 0)
            {
                foreach (Skill skill in Skill)
                    result.Append(string.Format("{0}", skill));
            }
            result.Append("</div>");

            result.Append("<div class='otherQualifications'>");
            result.Append("<h4>Övriga kvalifikationer:</h4>");
            if (OtherQualification.Count > 0)
            {
                foreach (OtherQualification otherQualification in OtherQualification)
                    result.Append(string.Format("{0}", otherQualification));
            }
            result.Append("</div>");

            result.Append("<div class='languages'>");
            result.Append("<h4>Språk:</h4>");
            if (Language.Count > 0)
            {
                foreach (Language language in Language)
                    result.Append(string.Format("{0}", language.ToHtml()));
            }
            result.Append("</div>");
            result.Append("</div>");
            return result.ToString();
        }

        public string PrintEducations()
        {
            StringBuilder result = new StringBuilder();
            result.Append("<div class='educations'>");
            result.Append("<h4>Utbildningar:</h4>");
            if (Education.Count > 0)
            {
                foreach (Education education in Education)
                    result.Append(string.Format("{0}", education.ToHtml()));
            }
            result.Append("</div>");
            return result.ToString();
        }

        public string PrintEmployments()
        {
            StringBuilder result = new StringBuilder();
            result.Append("<div class='employments'>");
            result.Append("<h4>Anställningar:</h4>");
            if (Employment.Count > 0)
            {
                foreach (Employment employment in Employment)
                    result.Append(string.Format("{0}", employment.ToHtml()));
            }
            result.Append("</div>");
            return result.ToString();
        }

        public string PrintSkills()
        {
            StringBuilder result = new StringBuilder();
            result.Append("<div class='skills'>");
            result.Append("<h4>Kompetencer:</h4>");
            if (Skill.Count > 0)
            {
                foreach (Skill skill in Skill)
                    result.Append(string.Format("{0}", skill));
            }
            result.Append("</div>");
            return result.ToString();
        }

        public string PrintOtherQualifications()
        {
            StringBuilder result = new StringBuilder();
            result.Append("<div class='otherQualifications'>");
            result.Append("<h4>Övriga kvalifikationer:</h4>");
            if (OtherQualification.Count > 0)
            {
                foreach (OtherQualification otherQualification in OtherQualification)
                    result.Append(string.Format("{0}", otherQualification));
            }
            result.Append("</div>");
            return result.ToString();
        }

        public string PrintLanguages()
        {
            StringBuilder result = new StringBuilder();
            result.Append("<div class='languages'>");
            result.Append("<h4>Språk:</h4>");
            if (Language.Count > 0)
            {
                foreach (Language language in Language)
                    result.Append(string.Format("{0}", language.ToHtml()));
            }
            result.Append("</div>");
            return result.ToString();
        }

    }
    public static class CurriculumVitaeDB
    {
        public static List<CurriculumVitae> GetAllCurriculumVitaes()
        {
            return DB.Context.CurriculumVitaes.Where(c => !c.IsDeleted).ToList();
        }

        public static CurriculumVitae GetCurriculumVitaeByApplicant(int applicantId)
        {
            return (DB.Context.CurriculumVitaes.Where(c => c.ApplicantUserId == applicantId)).SingleOrDefault(c => !c.IsDeleted);
        }

        public static List<CurriculumVitae> GetCurriculumVitaesByFreeTextSearchWords(string searchString)
        {
            var words = searchString.Split(' ');

            var allCurriculumVitaes = GetCurriculumVitaesByFreeTextSearchWord(words[0]);
            for (int i = 1; i < words.Length; i++)
            {
                allCurriculumVitaes = allCurriculumVitaes.Intersect(GetCurriculumVitaesByFreeTextSearchWord(words[i])).ToList();
            }
            return allCurriculumVitaes;
        }

        public static List<CurriculumVitae> GetCurriculumVitaesByFreeTextSearchWord(string searchWord)
        {

            var querySkill = GetSkillBySearchWord(searchWord);
            var queryOtherQualification = GetOtherQualificationBySearchWord(searchWord);
            var queryEmployment = GetEmploymentBySearchWord(searchWord);
            var queryEducation = GetEducationBySearchWord(searchWord);
            var queryLanguage = GetLanguageBySearchWord(searchWord);

            var query = querySkill.Union(queryOtherQualification);
            query = query.Union(queryEmployment);
            query = query.Union(queryEducation);
            query = query.Union(queryLanguage);

            query = query.Distinct().Where(c => !c.IsDeleted);
            return query.ToList();
        }

        public static List<CurriculumVitae> GetLanguageBySearchWord(string searchWord)
        {
            var queryLanguage =
                from c in DB.Context.CurriculumVitaes
                where c.Language.Any(l => l.LanguageName.Contains(searchWord) && !l.IsDeleted)
                select c;
            return queryLanguage.ToList();
        }

        public static List<CurriculumVitae> GetEducationBySearchWord(string searchWord)
        {
            var queryEducation =
                from c in DB.Context.CurriculumVitaes
                where c.Education.Any(e =>
                    e.EducationName.Contains(searchWord) ||
                    e.EducationDescription.Contains(searchWord) &&
                    !e.IsDeleted)
                select c;
            return queryEducation.ToList();
        }

        public static List<CurriculumVitae> GetEmploymentBySearchWord(string searchWord)
        {
            var queryEmployment =
                from c in DB.Context.CurriculumVitaes
                where c.Employment.Any(e =>
                    e.CompanyName.Contains(searchWord) ||
                    e.Titel.Contains(searchWord) ||
                    e.WorkDescription.Contains(searchWord) &&
                    !e.IsDeleted)
                select c;
            return queryEmployment.ToList();
        }

        public static List<CurriculumVitae> GetOtherQualificationBySearchWord(string searchWord)
        {
            var queryOtherQualification =
                from c in DB.Context.CurriculumVitaes
                where c.OtherQualification.Any(o => o.Organizer.Contains(searchWord) && !o.IsDeleted)
                select c;
            return queryOtherQualification.ToList();
        }

        public static List<CurriculumVitae> GetSkillBySearchWord(string searchWord)
        {
            var querySkill =
                from c in DB.Context.CurriculumVitaes
                where c.Skill.Any(s => s.SkillName.Contains(searchWord) && !c.IsDeleted)
                select c;
            return querySkill.ToList();
        }
    }
}
